package com.example.invoicing.controller;

import com.example.invoicing.pojo.PageResult;
import com.example.invoicing.pojo.SalesRecord;
import com.example.invoicing.service.SalesRecordService;
import com.example.invoicing.util.DateUtil;
import com.example.invoicing.util.ReturnUtil;
import com.example.invoicing.util.UuidUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping
@RestController
public class SalesRecordJsonController {
    @Autowired
    private SalesRecordService salesRecordService;

    //删除一条数据
    @ResponseBody
    @RequestMapping(value = "/member/deleteSalesRecord", method = RequestMethod.POST)
    public ModelMap deleteSalesRecord(@Validated @RequestBody SalesRecord salesRecord, BindingResult result) {
        try {

            if(StringUtils.isEmpty(salesRecord.getId())){
                return ReturnUtil.error("ID不能为空");
            }

            int count = salesRecordService.deleteByPrimaryKey(salesRecord.getId());
            if (count == 0){
                return ReturnUtil.error("删除失败");
            }

            return ReturnUtil.success("删除数据成功");

        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败");
        }
    }

    //添加一条数据
    @ResponseBody
    @RequestMapping(value = "/member/addSalesRecord", method = RequestMethod.POST)
    public ModelMap addSalesRecord(@Validated @RequestBody SalesRecord salesRecord, BindingResult result) {
        try {
            //随机产生一组加密字符
            salesRecord.setId(UuidUtil.getUUID());
            //获取到当前时间
            salesRecord.setCreateTime(DateUtil.getCurrentTime());
            int count = salesRecordService.insertSelective(salesRecord);
            if (count == 0){
                return ReturnUtil.success("添加数据失败");
            }

            return ReturnUtil.success("添加数据成功");

        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败");
        }
    }

    //查询一条数据
    @ResponseBody
    @RequestMapping(value = "/member/querySalesRecord", method = RequestMethod.POST)
    public ModelMap querySalesRecord(@Validated @RequestBody SalesRecord salesRecord, BindingResult result) {
        try {

            if(StringUtils.isEmpty(salesRecord.getId())){
                return ReturnUtil.error("ID不能为空");
            }

            SalesRecord salesRecord1 = salesRecordService.selectByPrimaryKey(salesRecord.getId());
            if (salesRecord1 == null){
                return ReturnUtil.error("查询数据失败");
            }

            return ReturnUtil.success("查询数据成功", salesRecord1);

        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败");
        }
    }

    //更新一条数据
    @ResponseBody
    @RequestMapping(value = "/member/updateSalesRecord", method = RequestMethod.POST)
    public ModelMap updateSalesRecord(@Validated @RequestBody SalesRecord salesRecord, BindingResult result) {
        try {

            if(StringUtils.isEmpty(salesRecord.getId())){
                return ReturnUtil.error("ID不能为空");
            }

            //更新修改时间
            salesRecord.setUpdateTime(DateUtil.getCurrentTime());

            int count = salesRecordService.updateByPrimaryKeySelective(salesRecord);
            if (count == 0){
                return ReturnUtil.error("更新数据失败");
            }

            return ReturnUtil.success("更新数据成功");

        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败");
        }
    }

    //集合于分页查询，所有查询，模糊查询
    @ResponseBody
    @RequestMapping(value = "/member/allSalesRecordList",method = RequestMethod.POST)
    public ModelMap allSalesRecordList(@Validated @RequestBody SalesRecord salesRecord, BindingResult result){
        try {

            List<SalesRecord> salesRecordList = salesRecordService.querySalesRecordList(salesRecord);
            if (salesRecordList.size() == 0){
                return ReturnUtil.error("查询失败,无该数据");
            }

            //查询数据有多少条
            int countLike = salesRecordService.queryCountLike(salesRecord);

            //加载实体将数据返回
            PageResult pageResult = new PageResult();
            if (salesRecord.getPageResult() != null){
                pageResult.setCurrentPage(salesRecord.getPageResult().getCurrentPage());
                pageResult.setPageSize(salesRecord.getPageResult().getPageSize());
            }

            pageResult.setTotalSize(countLike);
            pageResult.setItems(salesRecordList);

            return ReturnUtil.success("查询数据成功", pageResult);
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败");
        }
    }
}
